package com.ge.pp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;
import java.util.Vector;

public class PPDatabase {
	private String dbPath;
	private Hashtable<String, Vector<Hashtable<String,String>>> db;
	
	public PPDatabase(String _dbpath) {
		dbPath = _dbpath;
	}
	
	public void OpenDB() {
		try {
			System.out.println("Reading case database...");
			db = new Hashtable<String, Vector<Hashtable<String,String>>>();
			
			File f = new File(dbPath);
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
			String line = null;
			int state = 0;
			int row_count = 0;
			String sectionName = null;
			String [] headers=null;
			String [] fields=null;			
			Vector<Hashtable<String,String>> section = null;
			do {
				line = br.readLine();
				if(line != null) {					
					if(line.startsWith(">")) {
						// New section
						if (row_count != 0) {
							System.out.println("\tread "+row_count+" records");
						}
						sectionName = line.substring(1);
						System.out.println("Reading section:"+sectionName);
						row_count = 0;						
						section = new Vector<Hashtable<String,String>>();
						db.put(sectionName, section);
					} else if(line.startsWith("|")) {
						// Header fields
						headers = line.substring(1).split(",");						
					} else {					
						Hashtable<String,String> row = new Hashtable<String, String>();
						fields = line.split(",");
						for(int i=0; i<fields.length; i++) {
							row.put(headers[i], fields[i]);							
						}	
						row_count++;
						section.add(row);
					}
				}
			} while(line != null);
			if (row_count != 0)
				System.out.println("\tread "+row_count+" records");
			br.close();
		} catch(IOException e) {
			e.printStackTrace();
		}	
	}
	
	public Case GetCaseByID(int caseID) throws NumberFormatException, Exception {		
		Vector<Hashtable<String,String>> section = db.get("CASES");
		for(Hashtable<String, String> row:section) {
			if(row.get("caseid").equals(Integer.toString(caseID))) {				
				return new Case(Integer.parseInt(row.get("caseid")), 
								Integer.parseInt(row.get("patientid")), 
								row.get("casename"));
			}
		}
		return null;
	}
	
	public Patient GetPatientByID(int patientID) {		
		Vector<Hashtable<String,String>> section = db.get("PATIENTS");
		for(Hashtable<String, String> row:section) {
			if(row.get("patientid").equals(Integer.toString(patientID))) {
				return new Patient(Integer.parseInt(row.get("patientid")), row.get("firstname"), row.get("lastname"));
			}
		}
		return null;
	}
	
//	public int GetCaseIDByPatientID(int patientID) {
//		Vector<Hashtable<String,String>> section = db.get("CASE_PATIENTS");
//		for(Hashtable<String, String> row:section) {
//			if(row.get("patientid").equals(Integer.toString(patientID))) {
//				return Integer.parseInt(row.get("caseid"));
//			}
//		}
//		return -1;		
//	}
	
	public Vector<Integer> GetImageIDsByCaseID(int caseID) {
		Vector<Integer> imageList = new Vector<Integer>();
		Vector<Hashtable<String,String>> section = db.get("CASE_IMAGE");
		for(Hashtable<String, String> row:section) {
			if(row.get("caseid").equals(Integer.toString(caseID))) {
				imageList.add(Integer.parseInt(row.get("imageid")));
			}
		}
		return imageList;
	}
	
	public String GetImageDesc(int imageID) {
		Vector<Hashtable<String,String>> section = db.get("IMAGES");
		for(Hashtable<String, String> row:section) {
			if(row.get("imageid").equals(Integer.toString(imageID))) {				
				return row.get("imagedesc");
			}
		}
		return null;
	}
	
	public String GetImagePath(int imageID) {
		Vector<Hashtable<String,String>> section = db.get("IMAGES");
		for(Hashtable<String, String> row:section) {
			if(row.get("imageid").equals(Integer.toString(imageID))) {
				return row.get("imagepath");
			}
		}
		return null;
	}
	
	public Vector<Integer> GetCaseIDs() {
		Vector<Integer> v = new Vector<Integer>();
		Vector<Hashtable<String,String>> section = db.get("CASES");
		for(Hashtable<String, String> row:section) {
			v.add(Integer.parseInt(row.get("caseid")));
		}
		return v;
	}
	
	public Vector<Integer> GetAvailableCaseIDs() {
		Vector<Integer> v = new Vector<Integer>();
		Vector<Hashtable<String,String>> section = db.get("CASES");
		for(Hashtable<String, String> row:section) {
			if (row.get("assigned").equals("false"))
				v.add(Integer.parseInt(row.get("caseid")));
		}
		return v;
	}
	
	public void CheckoutCase(int caseID) {
		Vector<Hashtable<String,String>> section = db.get("CASES");		
		for(Hashtable<String, String> row:section) {
			if(row.get("caseid").equals(Integer.toString(caseID))) {
				row.put("assigned", "true");
				System.out.println("PPServer: Checking out case: "+caseID);
			}
		}
	}
	
	public void CheckinCase(int caseID) {
		Vector<Hashtable<String,String>> section = db.get("CASES");
		for(Hashtable<String, String> row:section) {
			if(row.get("caseid").equals(Integer.toString(caseID))) {
				row.put("assigned", "false");
				System.out.println("PPServer: Checking in case: "+caseID);
			}
		}			
	}
}
